'use strict';

require(['marionette', 'bootstrap'], function( Marionette) {
   window.App = new Backbone.Marionette.Application({
      debug: 0,
      templates: [],
      channelName: 'appChannel'
   });
   
   App.commands.setHandler("console", function(string, level){
      if (level > App.debug) {
         console.log(':' + string);
      }
   });
   
   App.commands.setHandler("loadcss", function(csstext){
      $(document).ready(function() {
         $('head').append('<style>' + csstext + '</style>');
      });
   });

   App.addRegions({
      headerRegion: '#main-header',
      sidebarRegion: '#main-sidebar-container'
   });
    
   App.on('before:start', function(options){
      App.execute('console', 'Application before:start event launched', 1);
   });
   
   App.on('start', function(options){
      App.execute('console', 'Application start event launched', 1);
   });

   require([
      'text!modules/header/templates/header_view.html'
      ], function(header_view){
         App.templates['header_view'] = header_view;
         require([
            'modules/header/views/header',
            'modules/header/header_module'
         ], function(){
            App.execute('console', 'header module finalized', 1);
         })
   });
   
   require([
      'text!modules/sidebar/templates/sidebaritem_view.html',
      'text!modules/sidebar/css/sidebar_item.css',
      'nicescroll'
      ], function(sidebaritem_view, sidebaritem_css){
         App.templates['sidebaritem_view'] = sidebaritem_view;
         App.execute('loadcss', sidebaritem_css);
         App.execute('console', App.templates['sidebaritem_view'], 1);
         require([
            'modules/sidebar/views/sidebar_item',
            'modules/sidebar/views/sidebaritem_container',
            'modules/sidebar/sidebar_module'
         ], function(){
            App.execute('console', 'sidebar module finalized', 1);
         })
   });
      
   App.execute('console', 'Application start event launched', 1);  
   if (Backbone.history){
         Backbone.history.start();
         App.execute('console', 'Application history started', 1);
   }
  
   var options = {};
   App.start(options);
});